/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import junit.framework.TestCase;
//import java.util.Map;
//import java.util.HashMap;
//import java.util.Map.Entry;

/**
 * Performs Validation Test for url validations.
 *
 * @version $Revision: 1128446 $ $Date: 2011-05-27 13:29:27 -0700 (Fri, 27 May 2011) $
 */
public class UrlValidatorTest extends TestCase {

   private boolean printStatus = false;
   private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.

   public UrlValidatorTest(String testName) {
      super(testName);
   }

   public void testManualTest()
   {
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   System.out.println("Manual tests-----------------------------------------------------------------");
	   
	   // test schemes
	   String url = "http://www.amazon.com";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "3tp://www.amazon.com";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url);

	   // test authority
	   url = "http://www.amazon";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://www.amazon.c";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://www.amazon.c:123";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://www.amazon.com:123";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://www.amazon.com:1234";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);

	   // test path
	   url = "http://www.amazon.com//file";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://www.amazon.com/test1/file";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   // test query
	   url = "http://www.amazon.com?userID=1";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://www.amazon.com?userID=1:name=bob";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);

	   // test fragment
	   url = "http://www.amazon.com#form";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);	   
   }
   
   // test partition between scheme and authority
   public void testYourFirstPartition()
   {
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   System.out.println("First Partition tests-----------------------------------------------------------------");
	   
	   // test various schemes with other URL components known to be valid
	   String url = "http://www.google.com";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http//www.google.com";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // no colon
	   
	   url = "ht3://www.google.com";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "HTTP://www.google.com";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "ftp/://www.google.com";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // slashes not allowed in scheme
	   
	   url = "4http://www.google.com"; 
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // first char must be letter
	   
	   url = "file:///path/to/file";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   // boundary testing between scheme and authority
	   url = "//www.google.com";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // no scheme
	   
	   url = "/www.google.com";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // no scheme
	   
	   url = "www.google.com";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // valid in a browser but technically invalid
	   
	   url = "http:/www.google.com";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // only one slash
	   
	   url = "http:///www.google.com";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // three slashes
	   
	   url = "file://path/to/file";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // file scheme needs three slashes
	   
	   // test on authority side of partition with known valid scheme
	   url = "http://google.com";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://com";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url);
   }
   
   // test partition between authority and path
   public void testYourSecondPartition(){
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   System.out.println("Second Partition tests-----------------------------------------------------------------");
	   
	   // test authority side of partition
	   String url = "http://www.google.com/";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://www.google.com:123/";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "file:///";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://www.google.c/";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // invalid top-level domain
	   
	   url = "http://www.google.c:123/";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // invalid top-level domain
	   
	   // test boundary between authority and path
	   url = "http://www.google.compath/to/file";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // missing slash between authority and path
	   
	   url = "http://www.google.com:123path/to/file";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // missing slash between authority and path
	   
	   url = "http://www.google.com/path/to/file";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://www.google.com:123/path/to/file";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "file:///path/to/file";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "file:////path/to/file";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // one too many slashes
	   
	   url = "http://www.google.com//path/to/file";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // one too many slashes
	   
	   // test path side of partition
	   url = "http://www.google.com/path//to/file";
	   System.out.println("Expected=false Actual=" + urlVal.isValid(url) + " url=" + url); // two slashes in a row
	   
	   url = "http://www.google.com/../file";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "file:///../file";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "http://www.google.com/./file";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
	   
	   url = "file:///./file";
	   System.out.println("Expected=true Actual=" + urlVal.isValid(url) + " url=" + url);
   }
   
   public void testIsValid()
   {
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
	   String url = "http://www.amazon.com//meyer#s.php";
	   System.out.println(urlVal.isValid(url));
	   /*for(int i = 0;i<10000;i++)
	   {
		   
	   }*/
   }
   /*
   public void testAnyOtherUnitTest()
   {
	   Map<String, Boolean> schemes = new HashMap<String, Boolean>();
	   schemes.put("http:", true);
	   schemes.put("http", false); // no colon
	   schemes.put("ht3:", true);
	   schemes.put("ftp/p:", false); // slashes not allowed
	   schemes.put("ftp:", true);
	   schemes.put("4http:", false); // only a-z for first character
	   schemes.put("foo+:", true);
	   
	   Map<String, Boolean> hostnames = new HashMap<String, Boolean>();
	   hostnames.put("//www.google.com", true);
	   hostnames.put("/www.google.com", false); // need two slashes at beginning
	   hostnames.put("//google.com", true);
	   hostnames.put("//10.10.10.10", true);
	   hostnames.put("//www.maps_google.com", false); // underscores not allowed
	   hostnames.put("www.google.com", false); // no leading slashes
	   hostnames.put("//www.google.c", false); // invalid domain
	   
	   Map<String, Boolean> ports = new HashMap<String, Boolean>();
	   ports.put("", true);
	   ports.put("123", false); // needs leading colon
	   ports.put(":123", true);
	   ports.put(":12345", true);
	   ports.put(":1234567", false); // only up to 5 digits allowed
	   ports.put(":12a", false); // no letters allowed
	   
	   Map<String, Boolean> paths = new HashMap<String, Boolean>();
	   paths.put("//path/to/file", false); // two slashes together not allowed
	   paths.put("/path/to/file", true);
	   paths.put("/../../file", true);
	   paths.put("/", true);
	   paths.put("path/to/file", false); // needs leading slash
	  
	   Map<String, Boolean> queries = new HashMap<String, Boolean>();
	   queries.put("", true);
	   queries.put("user=bob&id=1", true); 
	   queries.put("?user=bob", true);
	   queries.put("?user=bob;id=2", true);
	   
	   Map<String, Boolean> fragments = new HashMap<String, Boolean>();
	   fragments.put("#form", true);
	   fragments.put("#", true);
	   fragments.put("#form-id", true);
	   
	   int count = 0;
	   int errors = 0;
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
	   for (Entry<String, Boolean> scheme : schemes.entrySet()) {
		   boolean expected1 = scheme.getValue();
		   
		   for (Entry<String, Boolean> hostname : hostnames.entrySet()) {
			   boolean expected2 = hostname.getValue();
			   
			   for (Entry<String, Boolean> port : ports.entrySet()) {
				   boolean expected3 = port.getValue();
				   
				   for (Entry<String, Boolean> path : paths.entrySet()) {
					   boolean expected4 = path.getValue();
					   
					   for (Entry<String, Boolean> query : queries.entrySet()) {
						   boolean expected5 = query.getValue();
						   
						   for (Entry<String, Boolean> fragment : fragments.entrySet()) {
							   boolean expected6 = fragment.getValue();
							   String url = scheme.getKey() + hostname.getKey() + port.getKey() + path.getKey() + query.getKey() + fragment.getKey();
							   boolean expected = expected1 & expected2 & expected3 & expected4 & expected5 & expected6;
							   boolean actual = urlVal.isValid(url);
							   
							   if (actual != expected) {
								   System.out.println("actual=" + actual + " expected=" + expected + " " + url);
								   errors++;
							   }
							   count++;		   
						   }
					   }
				   }
			   }
		   }
	   }
	   System.out.println("Count=" + count + " errors=" + errors);
   }
   */
   /**
    * Create set of tests by taking the testUrlXXX arrays and
    * running through all possible permutations of their combinations.
    *
    * @param testObjects Used to create a url.
    */
   

}
